package com.iaiai.browser.activity.widget;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.iaiai.browser.activity.R;
import com.iaiai.browser.core.util.Utils;

/**
 * 
 * <p>
 * Title: IndexController.java
 * </p>
 * <p>
 * E-Mail: 176291935@qq.com
 * </p>
 * <p>
 * QQ: 176291935
 * </p>
 * <p>
 * Http: iaiai.iteye.com
 * </p>
 * <p>
 * Create time: 2011-11-2
 * </p>
 * 
 * @author 丸子
 * @version 0.0.1
 */
public class LabelManager {
	
	public interface Handler{
		void selected(int index);	//选择了哪个
		void loading();	//加载
		void unLoading();	//取消加载
		void remove(int index);	//删除
		void showIndex();	//显示首页
	}
	
	private static final int LABEL_SELECTED = R.drawable.t_selected;	//当前标签背景
	private static final int LABEL_NORMAL = R.drawable.t_normal;	//未选择的标签背景
	private static final int LABEL_PRESSED = R.drawable.t_pressed;	//获得焦点或是鼠标移上去的时候显示的背景，此地方没有用到此背景
	
	private LinearLayout labelLayout; // 标签

	private Context context;
	
	private Handler handler;	//事件
	
	private int selIndex = -1;	//当前选择的第几个，从0开始计算

	public void setHandler(Handler handler) {
		this.handler = handler;
	}

	public LabelManager(Context context) {
		this.context = context;
		
		init();
	}
	
	private Activity getActivity() {
		return (Activity) context;
	}

	private void init(){
		labelLayout = (LinearLayout) getActivity().findViewById(R.id.labelLayout);
	}
	
	/**
	 * 添加一个标签
	 */
	public void addLabel(String url) {
		selIndex = labelLayout.getChildCount() - 1;
		
		final LinearLayout layout = new LinearLayout(context);
		LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
		params.setMargins(0, Utils.dipTopx(context, 5), 0, 0);	//设置顶部距离
		layout.setLayoutParams(params);
		layout.setOrientation(LinearLayout.HORIZONTAL);
		
		ProgressBar progressBar = new ProgressBar(context);
		params = new LinearLayout.LayoutParams(Utils.dipTopx(context, 20),Utils.dipTopx(context, 20));
		params.setMargins(Utils.dipTopx(context, 25), Utils.dipTopx(context, 6), 0, 0);	//设置顶部距离
		progressBar.setIndeterminateDrawable(context.getResources().getDrawable(R.drawable.progressbar));
		progressBar.setLayoutParams(params);
		layout.addView(progressBar);	//添加到界面上
		
		ImageView iconView = new ImageView(context);
		params = new LinearLayout.LayoutParams(Utils.dipTopx(context, 20),Utils.dipTopx(context, 20));
		params.setMargins(Utils.dipTopx(context, 25), Utils.dipTopx(context, 6), 0, 0);	//设置顶部距离
		iconView.setLayoutParams(params);
		iconView.setVisibility(View.GONE);
//		iconView.setAdjustViewBounds(true);	//设置图片大小必须设置此参数，否则设置图片大小没用
//		iconView.setMaxHeight(20);
//		iconView.setMaxWidth(20);
		iconView.setImageDrawable(context.getResources().getDrawable(R.drawable.icon));
		layout.addView(iconView);	//添加到界面上
		
		TextView textView = new TextView(context);
		textView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT,1));
		textView.setTextColor(Color.BLACK);
		textView.setSingleLine();
		textView.setEllipsize(TextUtils.TruncateAt.END);
		textView.setGravity(Gravity.CENTER_VERTICAL|Gravity.CENTER);	//这句话为什么不管用呢
		textView.setPadding(0, Utils.dipTopx(context, 5), 0, 0);
		textView.setWidth(Utils.dipTopx(context, 60));
		textView.setText(url.replace("http://", ""));	//先显示url
		layout.addView(textView);	//添加到界面上
		
		ImageView closeView = new ImageView(context);
		params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
		params.setMargins(0, Utils.dipTopx(context, 8), Utils.dipTopx(context, 25), 0);	//设置顶部距离
		closeView.setLayoutParams(params);
		closeView.setAdjustViewBounds(true);	//设置图片大小必须设置此参数，否则设置图片大小没用
		closeView.setMaxHeight(Utils.dipTopx(context, 15));
		closeView.setMaxWidth(Utils.dipTopx(context, 15));
		closeView.setImageDrawable(context.getResources().getDrawable(R.drawable.close));
		closeView.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				int num = -1;
				for(int i = 0; i< labelLayout.getChildCount();i++){
					if(labelLayout.getChildAt(i)==layout){
						handler.remove(i+1);
						
						num=i;
						break;
					}
				}
				
				labelLayout.removeView(layout);	//删除
				
				if(num==-1)return;
				
				//显示
				if(num==0){
					//判断是否还有标签
					if(labelLayout.getChildCount()>0){
						//显示第一个
						show(1);
						handler.selected(1);
					}else{
						//显示首页
						handler.showIndex();
					}
				}else{
					show(num);
					handler.selected(num);
				}
			}
		});
		layout.addView(closeView);	//添加到界面上
		
		layout.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				for(int i = 0; i< labelLayout.getChildCount();i++){
					if(labelLayout.getChildAt(i)==layout){
						handler.selected(i+1);
						break;
					}
				}
				show(layout);
			}
		});
		
		labelLayout.addView(layout);	//添加到界面上
		
		show(labelLayout.getChildCount());	//显示最后一个
		
		scrollRight();	//跳转到最标签最右面
	}
	
	/**
	 * 设置icon
	 * @param index 第几个，从1开始
	 * @param icon 图片
	 */
	public void setIcon(int index,Bitmap icon){
		LinearLayout ll = (LinearLayout)labelLayout.getChildAt(index-1);
		
		try{
			((ImageView)ll.getChildAt(1)).setImageBitmap(icon);
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	/**
	 * 设置标签标题
	 * @param index 第几个，从1开始
	 * @param title 标题
	 */
	public void setTitle(int index,String title){
		LinearLayout ll = (LinearLayout)labelLayout.getChildAt(index-1);
		
		try{
			((TextView)ll.getChildAt(2)).setText(title);
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	/**
	 * 跳转到最标签最右面
	 */
	private void scrollRight(){
		final HorizontalScrollView horizontalScrollView = (HorizontalScrollView) getActivity().findViewById(R.id.horizontalScrollView); // 跳转到布局最右面
		horizontalScrollView.post(new Runnable() { 
	        public void run() { 
	        	horizontalScrollView.fullScroll(View.FOCUS_RIGHT); 
	        } 
		});
	}
	
	private void show(LinearLayout layout){
		for(int i = 0;i<labelLayout.getChildCount();i++){
			if(labelLayout.getChildAt(i)==layout){
				selIndex = i;	//设置当前选择
				layout.setBackgroundDrawable(getActivity().getResources().getDrawable(LABEL_SELECTED));	//设置选中
			}else{
				labelLayout.getChildAt(i).setBackgroundDrawable(getActivity().getResources().getDrawable(LABEL_NORMAL));
			}
		}
	}
	
	/**
	 * 显示第几个标签
	 * @param index 从1开始
	 */
	public void show(int index){
		selIndex = index - 1;	//设置当前选择
		for(int i = 0;i<labelLayout.getChildCount();i++){
			labelLayout.getChildAt(i).setBackgroundDrawable(getActivity().getResources().getDrawable(i+1==index?LABEL_SELECTED:LABEL_NORMAL));
		}
	}
	
	/**
	 * 获得当前选择的标题
	 * @return
	 */
	public String getSelTitle(){
		return ((TextView)((LinearLayout)labelLayout.getChildAt(selIndex)).getChildAt(2)).getText().toString();
	}
	
	/**
	 * 显示loading...
	 * @param index 第几个，从1开始
	 */
	public void showLoading(int index){
		((LinearLayout)labelLayout.getChildAt(index-1)).getChildAt(0).setVisibility(View.VISIBLE);
		((LinearLayout)labelLayout.getChildAt(index-1)).getChildAt(1).setVisibility(View.GONE);
	}
	
	/**
	 * 隐藏loading...
	 * @param index 第几个，从1开始
	 */
	public void hiddenLoading(int index){
		((LinearLayout)labelLayout.getChildAt(index-1)).getChildAt(0).setVisibility(View.GONE);
		((LinearLayout)labelLayout.getChildAt(index-1)).getChildAt(1).setVisibility(View.VISIBLE);
	}
	
}
